package cn.amarone.wineblog.util;

import cn.amarone.wineblog.common.appointment.Assert;
import cn.amarone.wineblog.common.appointment.Result;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ServletUtils {

  public static String renderString(HttpServletResponse response, Result res) {
    Assert.isNullOrEmpy(res);
    return renderString(response, res.toString());
  }

  /**
   * 将字符串渲染到客户端
   *
   * @param response 渲染对象
   * @param string 待渲染的字符串
   * @return null
   */
  public static String renderString(HttpServletResponse response, String string) {
    try {
      response.setStatus(200);
      response.setContentType("application/json");
      response.setCharacterEncoding("utf-8");
      response.getWriter().print(string);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }
}
